dsub<-data.frame(svymean(make.formula(vars),dsub,na.rm=TRUE))
dsub$var<-rownames(dsub)
dsub%<>%separate(var,into=c("var","term"),sep="_",remove=TRUE)
names(dsub)[1:2]<-c("estimate","std.error")
dsub$sample<-sample
return(dsub)
}
protest.fb.wtd<-demosum.wtd("Protested")
meeting.fb.wtd<-demosum.wtd("Meeting")%>%mutate(var="Attended a meeting")
petition.fb.wtd<-demosum.wtd("Petition")%>%mutate(var="Signed a petition")
frequentvoter.fb.wtd<-demosum.wtd("frequentvoter")%>%mutate(var="Frequent voter")
presidentparty.fb.wtd<-demosum.wtd("presidentparty",sample="Facebook (weighted)")%>%
mutate(var="President's party")
online.fb.wtd<-demosum.wtd("OnlinePost")%>%
mutate(var="Posted online")
happening.fb.wtd<-demosum.wtd("gwhappening")
human.fb.wtd<-demosum.wtd("gwhuman")
important.fb.wtd<-demosum.wtd("gwimportant")
vote.fb.wtd<-demosum.wtd("votelastelection")%>%mutate(var="Voted last election")
## AB political summaries ####
asiabaro<-read_csv("data_cleaned/asianbarometer_cleaned.csv")
protest.ab<-demosum.unwtd(var="Protested",sample="Asiabarometer",data=asiabaro)
meeting.ab<-demosum.unwtd("Meeting","Asiabarometer",asiabaro)%>%mutate(var="Attended a meeting")
petition.ab<-demosum.unwtd("Petition","Asiabarometer",asiabaro)%>%mutate(var="Signed a petition")
online.ab<-demosum.unwtd("OnlinePost","Asiabarometer",asiabaro)%>%
mutate(var="Posted online")
voting.ab<-demosum.unwtd("votelastelection","Asiabarometer",asiabaro)%>%mutate(var="Voted last election")
asiabaro%<>%mutate(frequentvoter=ifelse(votinghabit%in%c("most","every"),"frequent","infrequent"))
frequentvoter.ab<-demosum.unwtd("frequentvoter","Asiabarometer",asiabaro)%>%mutate(var="Frequent voter")
asiabaro%<>%mutate(presidentparty=ifelse(party=="PDI-P","pdip","otherparty"))
presidentparty.ab<-demosum.unwtd("presidentparty","Asiabarometer",asiabaro)%>%mutate(var="President's party")
## dynata political summaries ####
dynata<-read_csv("data_cleaned/dynata_cleaned.csv")
dynata%<>%
mutate(gw_human=case_when(gw_human=="Caused mostly by human activities"~"Human",
gw_human=="Caused mostly by natural changes in the environment"~"Natural",
grepl("None",gw_human)==TRUE~"Nothappening"),
gw_important=gsub(" ","",gw_important))
dynata%<>%rename(gwhappening=gw_happen,
gwhuman=gw_human,
gwimportant=gw_important)
table(dynata$gwhappening)
table(dynata$gwhuman)
table(dynata$gwimportant)
human.dynata.unwtd<-demosum.unwtd("gwhuman",sample="Dynata (unweighted)",data=dynata)
human.dynata.wtd<-demosum.wtd("gwhuman","Dynata (weighted)",dynata$weight_trimmed,dynata)
happening.dynata.unwtd<-demosum.unwtd("gwhappening",sample="Dynata (unweighted)",data=dynata)
happening.dynata.wtd<-demosum.wtd("gwhappening","Dynata (weighted)",dynata$weight_trimmed,dynata)
important.dynata.unwtd<-demosum.unwtd("gwimportant",sample="Dynata (unweighted)",data=dynata)
important.dynata.wtd<-demosum.wtd("gwimportant","Dynata (weighted)",dynata$weight_trimmed,dynata)
dynata%<>%mutate(frequentvoter=ifelse(votinghabit%in%c("most","every"),"frequent","infrequent"))
frequentvoter.dynata.unwtd<-demosum.unwtd("frequentvoter","Dynata (unweighted)",dynata)%>%mutate(var="Frequent voter")
frequentvoter.dynata.wtd<-demosum.wtd("frequentvoter","Dynata (weighted)",dynata$weight_trimmed,dynata)%>%mutate(var="Frequent voter")
voting.census<-data.frame(estimate=.791,std.error=0,var="Voted last election",term="Yes",sample="Election results") ## note that this doesn't actually come from census
political<-do.call(rbind,list(online.ab,petition.ab,protest.ab,meeting.ab,
online.fb.unwtd,petition.fb.unwtd,protest.fb.unwtd,meeting.fb.unwtd,
online.fb.wtd,petition.fb.wtd,protest.fb.wtd,meeting.fb.wtd,voting.census,
voting.ab,vote.fb.unwtd,vote.fb.wtd,
frequentvoter.ab,frequentvoter.dynata.unwtd,frequentvoter.dynata.wtd,
frequentvoter.fb.unwtd,frequentvoter.fb.wtd,
human.fb.unwtd,human.fb.wtd,happening.fb.unwtd,happening.fb.wtd,
important.fb.unwtd,important.fb.wtd,human.dynata.unwtd,human.dynata.wtd,
happening.dynata.unwtd,happening.dynata.wtd,important.dynata.unwtd,important.dynata.wtd,
presidentparty.ab,presidentparty.fb.unwtd,presidentparty.fb.wtd))
## manually add a "yes" row with estimate=0 for petition signers from AB survey
political<-rbind(political,data.frame(estimate=0,std.error=0,var="Signed a petition",term="Yes",sample="Asiabarometer")) ## REMEMBER TO NOTE THIS IN THE WRITEUP
## reformat political object so that it can be made into figures ##
political%<>%
filter(term!="No",term!="DK",!is.na(term),term!="NA",
term!="Nothappening",term!="Natural")%>% ## create single values for "global warming is happening" and political action questions
mutate(term=ifelse(term=="yes"|term=="Yes",var,term),
# var=ifelse(var=="partycollapsed","party",var),
term=case_when(term=="Human"~"Climate change is human caused",
# term=="Natural"~"Climate change is due to natural causes",
# term=="Nothappening"~"Climate change is not happening",
term=="gwhappening"~"Climate change is happening",
term=="Extremelyimportant"~"Climate change extremely important",
term=="Notatallimportant"~"Climate change not at all important",
term=="Nottooimportant"~"Climate change not too important",
term=="Somewhatimportant"~"Climate change somewhat important",
term=="Veryimportant"~"Climate change very important",
term=="frequent"~"Vote in most or every election",
term=="infrequent"~"Rarely or never vote",
term=="pdip"~"President's party (PDI-P)",
term=="otherparty"~"Other party",
var=="Voted last election"~"Voted in 2019 election",
TRUE~term))
write_csv(political,"data_exports/politicalparticipation_indonesia_wave1.csv")
# non-response bias: relationship between facebook demographics and public opinion/ political behavior ####
## clean up a new data object with the political variables
dpol<-d%>%
select(fb_gender,fb_agegroup,Protested,Meeting,Petition,frequentvoter,presidentparty,gwimportant,
gwhappening,gwhuman)%>%
mutate(across(Protested:Petition,~ifelse(.=="Yes",1,0)),
frequentvoter=ifelse(frequentvoter=="frequent",1,0),
presidentparty=ifelse(presidentparty=="otherparty",0,1),
gwimportant=ifelse(gwimportant%in%c("Extremelyimportant","Veryimportant"),1,0),
gwhappening=ifelse(gwhappening=="Yes",1,0),
gwhuman=ifelse(gwhuman=="Human",1,0))%>%
filter(!is.na(fb_gender),!is.na(fb_agegroup))
dpol%<>%mutate(fb_agegroup=relevel(factor(fb_agegroup),ref="60+"))
lms<-feols(c(Protested,Meeting,Petition,frequentvoter,presidentparty,gwhuman)~fb_gender+fb_agegroup,dpol)
etable(lms,se="hetero",tex=TRUE,fontsize="small",digits=2,
dict=c(`fb_genderMale`="Male",`fb_agegroup30-49`="30-49",`fb_agegroup50-59`="50-59",`fb_agegroup21-29`="21-29"))
table(d$gwhuman)
# demographic summaries ####
## FB demographic summaries ####
d%<>%
rename(demogender=demo_gender,
demoreligion=dem_rel,
demoeducation=degreerecode,
demoagegroup=demo_agegroup)%>%
mutate(demoagegroup=gsub("-","",demoagegroup),
demoagegroup=ifelse(demoagegroup=="60+","60",demoagegroup))
political
d%<>%mutate(demoreligionrecode=case_when(demoreligion%in%c("Christian","Catholic")~"Christian",
demoreligion%in%c("Buddha","Confucianism","Hindus","Other")~"Other",
demoreligion=="Islam"~"Muslim",
TRUE~demoreligion))
table(d$demoreligionrecode)
table(d$dem_maritial)
d%<>%mutate(marital=case_when(dem_maritial%in%c("Divorced","Separated")~"Divorced",
dem_maritial%in%c("Living together as married","Married")~"Married",
dem_maritial%in%c("Single","Widowed")~"Single"))
table(d$marital)
table(d$demoeducation)
marital.fb.unwtd<-demosum.unwtd("marital")
gender.fb.unwtd<-demosum.unwtd("demogender")
religion.fb.unwtd<-demosum.unwtd("demoreligionrecode")
education.fb.unwtd<-demosum.unwtd("demoeducation")%>%
mutate(term=case_when(term=="Postsecondary"~"College degree or higher",
term=="secondary"~"Secondary degree",
term=="nosecondary"~"Did not complete secondary school"))
agegroup.fb.unwtd<-demosum.unwtd("demoagegroup")
marital.fb.wtd<-demosum.wtd("marital")
gender.fb.wtd<-demosum.wtd("demogender")
religion.fb.wtd<-demosum.wtd("demoreligionrecode")
education.fb.wtd<-demosum.wtd("demoeducation")%>%
mutate(term=case_when(term=="Postsecondary"~"College degree or higher",
term=="secondary"~"Secondary degree",
term=="nosecondary"~"Did not complete secondary school"))
agegroup.fb.wtd<-demosum.wtd("demoagegroup")
## FB ad clickers demographic summaries ####
adclickers%<>%filter(!is.na(fb_agegroup),!is.na(fb_gender))%>%
mutate(fb_agegroup=gsub("_","",fb_agegroup),
fb_agegroup=gsub("-","",fb_agegroup),
fb_agegroup=ifelse(fb_agegroup=="60+","60",fb_agegroup))%>%
rename(fbagegroup=fb_agegroup,
fbgender=fb_gender)
agegroup.adclickers<-demosum.unwtd(var="fbagegroup",sample="Facebook (ad clickers)",data=adclickers)
gender.adclickers<-demosum.unwtd(var="fbgender",sample="Facebook (ad clickers)",data=adclickers)
## AB demographic summaries ####
asiabaro%<>%
mutate(agegroup=gsub("-","",agegroup),
agegroup=ifelse(agegroup=="60+","60",agegroup))%>%
filter(!is.na(agegroup)) ## lose 24 responses here
asiabaro%<>%mutate(religionrecode=
case_when(religion=="Islam"~"Muslim",
religion%in%c("Buddha","Confucianism","Hindu","Others")~"Other",
religion%in%c("Protestant","Catholic")~"Christian"))
gender.ab<-demosum.unwtd(var="gender",sample="Asiabarometer",data=asiabaro)
religion.ab<-demosum.unwtd(var="religionrecode",sample="Asiabarometer",data=asiabaro)
asiabaro%<>%rename(demoeducation=dem_edu)
asiabaro%<>%
mutate(edrecode=case_when(demoeducation%in%c("None","Primary")~"nosecondary",
demoeducation=="Secondary"~"secondary",
demoeducation=="University"~"Postsecondary",
TRUE~demoeducation))
table(asiabaro$edrecode)
education.ab<-demosum.unwtd(var="edrecode",sample="Asiabarometer",data=asiabaro)%>%
mutate(term=case_when(term=="Postsecondary"~"College degree or higher",
term=="secondary"~"Secondary degree",
term=="nosecondary"~"Did not complete secondary school"))
agegroup.ab<-demosum.unwtd(var="agegroup",sample="Asiabarometer",data=asiabaro)
# dynata demographic summaries ####
dynata%<>%
rename(demogender=demo_gender,
demoeducation=degreerecode,
demoagegroup=demo_agegroup)
dynata%<>%mutate(
demoagegroup=gsub("-","",demoagegroup),
demoagegroup=ifelse(demoagegroup=="60+","60",demoagegroup)
)
gender.dynata.wtd<-demosum.wtd("demogender",sample="Dynata (weighted)",weights=dynata$weight_trimmed,data=dynata)
education.dynata.wtd<-demosum.wtd("demoeducation",sample="Dynata (weighted)",weights=dynata$weight_trimmed,data=dynata)%>%
mutate(term=case_when(term=="Postsecondary"~"College degree or higher",
term=="secondary"~"Secondary degree",
term=="nosecondary"~"Did not complete secondary school"))
agegroup.dynata.wtd<-demosum.wtd("demoagegroup",sample="Dynata (weighted)",weights=dynata$weight_trimmed,data=dynata)
gender.dynata.unwtd<-demosum.unwtd("demogender",sample="Dynata (unweighted)",data=dynata)
education.dynata.unwtd<-demosum.unwtd("demoeducation",sample="Dynata (unweighted)",data=dynata)%>%
mutate(term=case_when(term=="Postsecondary"~"College degree or higher",
term=="secondary"~"Secondary degree",
term=="nosecondary"~"Did not complete secondary school"))
agegroup.dynata.unwtd<-demosum.unwtd("demoagegroup",sample="Dynata (unweighted)",data=dynata)
table(dynata$dem_marital)
dynata%<>%mutate(marital=case_when(dem_marital%in%c("Divorced","Separated")~"Divorced",
dem_marital%in%c("Single","Widowed")~"Single",
dem_marital%in%c("Married","Living together as married")~"Married"))
marital.dynata.wtd<-demosum.wtd("marital","Dynata (weighted)",dynata$weight_trimmed,dynata)
marital.dynata.unwtd<-demosum.unwtd("marital","Dynata (unweighted)",dynata)
## census demographics ####
## merge in # of households
households<-read_csv("data_cleaned/indonesia_census/households.csv")
households%<>%
mutate(households=`households (thousands)`*1000,
Province=str_to_title(Province),
Province=case_when(Province=="Kep. Bangka Belitung"~"Kepulauan Bangka Belitung",
Province=="Kep. Riau"~"Kepulauan Riau",
Province=="Dki Jakarta"~"Daerah Khusus Ibu Kota Jakarta",
Province=="Di Yogyakarta"~"Daerah Istimewa Yogyakarta",
TRUE~Province))%>%
filter(Province!="Indonesia",!is.na(Province))#%>%
# marital status from census
marital.census<-read_csv("data_cleaned/indonesia_census/marital.csv")
## census demographics ####
## merge in # of households
households<-read_csv("data/indonesia/households.csv")
households%<>%
mutate(households=`households (thousands)`*1000,
Province=str_to_title(Province),
Province=case_when(Province=="Kep. Bangka Belitung"~"Kepulauan Bangka Belitung",
Province=="Kep. Riau"~"Kepulauan Riau",
Province=="Dki Jakarta"~"Daerah Khusus Ibu Kota Jakarta",
Province=="Di Yogyakarta"~"Daerah Istimewa Yogyakarta",
TRUE~Province))%>%
filter(Province!="Indonesia",!is.na(Province))#%>%
# marital status from census
marital.census<-read_csv("data/indonesia/marital.csv")
marital.census%<>%mutate(Province=gsub("[[:digit:]]+","",Province),
Province=gsub("[[:punct:]]","",Province),
Province=str_squish(Province))%>%
filter(!is.na(Province))
marital.total<-sum(marital.census$Total)
marital.census%<>%
select(-Total)%>%
filter(!is.na(Province))%>%
pivot_longer(Single:Widowed,names_to="term",values_to="freq")%>%
mutate(term=ifelse(term=="Widowed","Single",term))%>%
group_by(term)%>%
summarise(estimate=sum(freq)/marital.total,
std.error=0,
var="marital",
sample="Census")
lighting.census<-read_csv("data/indonesia/lighting.csv")
setdiff(lighting.census$Province,unique(households$Province)) ## demo_region is the province that people self-selected as their residence, NOT recoded to correspond with the set of values used in the ad sets to assign provinces
lighting.census%<>%
mutate(Province=case_when(Province=="DKI Jakarta"~"Daerah Khusus Ibu Kota Jakarta",
Province=="DI Yogyakarta"~"Daerah Istimewa Yogyakarta",
TRUE~Province))%>%
filter(Province!="Indonesia",!is.na(Province))
lighting.census%<>%
left_join(households,by=c("Province"))%>%
mutate(across(contains("electricity"),~(.x*households)/100))
households.total<-sum(lighting.census$households)
lighting.census%<>%select(contains("electricity"))%>%
summarise_all(~(sum(.))/households.total)
lighting.census<-data.frame(estimate=t(lighting.census),
var="light",
sample="Census",
std.error=0)
lighting.census$term<-rownames(lighting.census)
lighting.census%<>%
filter(term!="No electricity")%>%
mutate(term="Electricity")%>%
group_by(term,var,sample,std.error)%>%
summarise(estimate=sum(estimate))%>%
ungroup()
## summarise facebook lighting responses
d%<>%rename(light=hh_light)%>%
mutate(light=case_when(light=="PLN electricity with meter"|light=="PLN electricity without meter"~"Electricity",
light=="No electricity"~"None",
light=="Non-PLN electricity"~"Electricity"))
lighting.fb.wtd<-demosum.wtd("light")%>%
filter(term!="None",!is.na(term),term!="NA")
lighting.fb.unwtd<-demosum.unwtd("light")%>%
filter(term!="None",!is.na(term),term!="NA")
## cell phone data from census
cell.census<-read_csv("data/indonesia/cellphone.csv")%>%
mutate(Province=str_to_title(Province))
setdiff(cell.census$Province,households$Province)
sort(unique(households$Province))
cell.census%<>%
mutate(Province=case_when(Province=="Kep. Bangka Belitung"~"Kepulauan Bangka Belitung",
Province=="Kep. Riau"~"Kepulauan Riau",
Province=="Dki Jakarta"~"Daerah Khusus Ibu Kota Jakarta",
Province=="Di Yogyakarta"~"Daerah Istimewa Yogyakarta",
TRUE~Province))%>%
filter(Province!="Indonesia",!is.na(Province))
cell.census%<>%
left_join(households,by=c("Province"))%>%
mutate(cellphone=(cellphone*households)/100)
cell.census<-data.frame(estimate=sum(cell.census$cellphone)/households.total,
term="Owns cell phone",
var="cell",
sample="Census",
std.error=0)
## religion data from census
## population from census
regionrecodes<-d%>%
select(demo_region,demo_region_recode)%>%distinct()
religion.census<-read_csv("data/indonesia/religion.csv")%>%
filter(!is.na(province))%>%
left_join(regionrecodes,by=c("province"="demo_region_recode"))
religion.census%<>%
select(-province)%>%
pivot_longer(-demo_region,values_to="Freq",names_to="religion")%>%
mutate(term=case_when(religion=="Christian"|religion=="Catholic"~"Christian",
religion=="Islam"~"Muslim",
religion%in%c("Christian","Catholic","Islam")==FALSE~"Other"))%>%
group_by(term)%>%
summarise(Freq=sum(Freq))
religiontotal<-sum(religion.census$Freq)
religion.census%<>%
mutate(estimate=Freq/religiontotal,
var="demoreligionrecode",
sample="Census",
std.error=0)%>%
select(-Freq)
rm(cell.census)
## gender, age data from census
load("data_exports/weightingdata_indonesia.Rda")
gender.census<-gender%>%
mutate(estimate=Freq/sum(gender$Freq),
std.error=0,
var="gender",
sample="Census")%>%
rename(term=demo_gender)%>%
select(-Freq)
age.census<-age%>%
mutate(estimate=Freq/sum(age$Freq),
std.error=0,
var="agegroup",
sample="Census")%>%
rename(term=demo_agegroup)%>%
select(-Freq)
degree.census%<>%
mutate(term=case_when(term=="Postsecondary"~"College degree or higher",
term=="secondary"~"Secondary degree",
term=="nosecondary"~"Did not complete secondary school"))
View(political)
demosum<-do.call(rbind,list(lighting.fb.wtd,lighting.census,lighting.fb.unwtd,#cell.fb.unwtd,
#cell.fb.wtd,
cell.census,degree.census,education.fb.wtd,education.fb.unwtd,
gender.fb.unwtd,gender.fb.wtd,gender.ab,
religion.fb.unwtd,religion.fb.wtd,religion.ab,religion.census,
#education.fb.unwtd,education.fb.wtd, ### NOTE IN PAPER THAT
education.ab,
gender.dynata.wtd,gender.dynata.unwtd,
education.dynata.wtd,education.dynata.unwtd,
agegroup.dynata.wtd,agegroup.dynata.unwtd,
agegroup.ab,agegroup.fb.unwtd,agegroup.fb.wtd,
gender.census,age.census,marital.fb.unwtd,marital.fb.wtd,
marital.dynata.unwtd,marital.dynata.wtd,
marital.census#,gender.adclickers,agegroup.adclickers
))%>%
mutate(ci.high=estimate+1.96*std.error,
ci.low=estimate-1.96*std.error)%>%
filter(term!="Male")%>%
mutate(term=case_when(
term=="60"~"60+",
term=="3049"~"30-49",
term=="5059"~"50-59",
term=="1829"~"18-29",
term=="2129"~"21-29",
term=="Electricity"~"Electrified household",
term=="Owns cell phone"~"Household owns cell phone",
term=="Other"~"Other religion",
TRUE~term))
demosum<-do.call(rbind,list(lighting.fb.wtd,lighting.census,lighting.fb.unwtd,degree.census,education.fb.wtd,education.fb.unwtd,
gender.fb.unwtd,gender.fb.wtd,gender.ab,
religion.fb.unwtd,religion.fb.wtd,religion.ab,religion.census,
#education.fb.unwtd,education.fb.wtd, ### NOTE IN PAPER THAT
education.ab,
gender.dynata.wtd,gender.dynata.unwtd,
education.dynata.wtd,education.dynata.unwtd,
agegroup.dynata.wtd,agegroup.dynata.unwtd,
agegroup.ab,agegroup.fb.unwtd,agegroup.fb.wtd,
gender.census,age.census,marital.fb.unwtd,marital.fb.wtd,
marital.dynata.unwtd,marital.dynata.wtd,
marital.census#,gender.adclickers,agegroup.adclickers
))%>%
mutate(ci.high=estimate+1.96*std.error,
ci.low=estimate-1.96*std.error)%>%
filter(term!="Male")%>%
mutate(term=case_when(
term=="60"~"60+",
term=="3049"~"30-49",
term=="5059"~"50-59",
term=="1829"~"18-29",
term=="2129"~"21-29",
term=="Electricity"~"Electrified household",
term=="Owns cell phone"~"Household owns cell phone",
term=="Other"~"Other religion",
TRUE~term))
write_csv(demosum,"data_exports/demographics_indonesia.csv")
demosum<-do.call(rbind,list(degree.census,education.fb.wtd,education.fb.unwtd,
gender.fb.unwtd,gender.fb.wtd,gender.ab,
religion.fb.unwtd,religion.fb.wtd,religion.ab,religion.census,
education.ab,
gender.dynata.wtd,gender.dynata.unwtd,
education.dynata.wtd,education.dynata.unwtd,
agegroup.dynata.wtd,agegroup.dynata.unwtd,
agegroup.ab,agegroup.fb.unwtd,agegroup.fb.wtd,
gender.census,age.census,marital.fb.unwtd,marital.fb.wtd,
marital.dynata.unwtd,marital.dynata.wtd,
marital.census#,gender.adclickers,agegroup.adclickers
))%>%
mutate(ci.high=estimate+1.96*std.error,
ci.low=estimate-1.96*std.error)%>%
filter(term!="Male")%>%
mutate(term=case_when(
term=="60"~"60+",
term=="3049"~"30-49",
term=="5059"~"50-59",
term=="1829"~"18-29",
term=="2129"~"21-29",
term=="Electricity"~"Electrified household",
term=="Owns cell phone"~"Household owns cell phone",
term=="Other"~"Other religion",
TRUE~term))
write_csv(demosum,"data_exports/demographics_indonesia.csv")
# save framing:
table(d1$prospect_gain)/length(na.omit(d1$prospect_gain))
# die framing:
table(d1$prospect_loss)/length(na.omit(d1$prospect_loss))
## weighted version to run ##
## gain/save ##
# Subset data for relevant categories
subset_save <- d1[d1$prospect_gain %in% c("Program A", "Program B"), ]
# Calculate weighted counts
weighted_save <- with(subset_save, tapply(weight_trimmed, prospect_gain, sum))
# Calculate proportions
weighted_prop_save <- weighted_save / sum(weighted_save)
## loss/die ##
# Subset data for relevant categories
subset_die <- d1[d1$prospect_loss %in% c("Program A", "Program B"), ]
# Calculate weighted counts
weighted_die <- with(subset_die, tapply(weight_trimmed, prospect_loss, sum))
# Calculate proportions
weighted_prop_die <- weighted_die / sum(weighted_die)
## t test for the die column
subset_die%<>%
select(ResponseId,weight_trimmed,prospect_loss)%>%
mutate(choice=1)%>%
pivot_wider(id_cols=c(ResponseId,weight_trimmed),names_from=prospect_loss,values_from=choice)%>%
mutate(across(`Program A`:`Program B`,~ifelse(is.na(.),0,.)))
# t.test(x=subset_die$prospect_loss[])
library(weights)
wtd.t.test(x=subset_die$`Program A`,
y=subset_die$`Program B`,
weight=subset_die$weight_trimmed,
weighty=subset_die$weight_trimmed)
t.test(x=subset_die$`Program A`,y=subset_die$`Program B`,alternative=c("two.sided"))
## t test for the save column
subset_save%<>%
select(ResponseId,weight_trimmed,prospect_gain)%>%
mutate(choice=1)%>%
pivot_wider(id_cols=c(ResponseId,weight_trimmed),names_from=prospect_gain,values_from=choice)%>%
mutate(across(`Program A`:`Program B`,~ifelse(is.na(.),0,.)))
# t.test(x=subset_die$prospect_loss[])
wtd.t.test(x=subset_save$`Program A`,
y=subset_save$`Program B`,
weight=subset_save$weight_trimmed,
weighty=subset_save$weight_trimmed)
## results among sub-groups of respondents to figure out where the null result in the die condition is coming from
table(d1$prospect_loss[d1$demo_educ!="Tertiary education (college or university degree)"])/length(na.omit(d1$prospect_loss[d1$demo_educ!="Tertiary education (college or university degree)"]))
table(d1$prospect_loss[d1$demo_educ=="Tertiary education (college or university degree)"])/length(na.omit(d1$prospect_loss[d1$demo_educ=="Tertiary education (college or university degree)"])) ## no difference by education
table(d1$prospect_loss[d1$demo_agegroup%in%c("18-29","30-49")==FALSE])/length(na.omit(d1$prospect_loss[d1$demo_agegroup%in%c("18-29","30-49")==FALSE]))## this is the difference
table(d1$prospect_loss[d1$demo_agegroup%in%c("18-29","30-49")==TRUE])/length(na.omit(d1$prospect_loss[d1$demo_agegroup%in%c("18-29","30-49")==TRUE]))## this is the difference
table(d1$prospect_loss[d1$demo_gender=="Female"])/length(na.omit(d1$prospect_loss[d1$demo_gender=="Female"])) ## no differences by gender
table(d1$prospect_loss[d1$demo_gender!="Female"])/length(na.omit(d1$prospect_loss[d1$demo_gender!="Female"])) ## no differences by gender
## average weight for the older individuals:
mean(d1$weight_trimmed[d1$demo_agegroup%in%c("50-59","60+")])
weighted_die
weighted_prop_die
weighted_prop_save
## results among sub-groups of respondents for Table S3
table(d1$prospect_loss[d1$demo_educ!="Tertiary education (college or university degree)"])/length(na.omit(d1$prospect_loss[d1$demo_educ!="Tertiary education (college or university degree)"]))
### analysis for indonesia ##
# preamble ####
rm(list=ls())
setwd("~/Dropbox/facebook sampling/replication")
library(foreign)
library(survey)
library(xtable)
library(magrittr)
library(readxl)
library(haven)
library(fixest)
install.packages("fixest")
### analysis for indonesia ##
# preamble ####
rm(list=ls())
setwd("~/Dropbox/facebook sampling/replication")
library(foreign)
library(survey)
library(xtable)
library(magrittr)
library(readxl)
library(haven)
library(fixest)
library(sandwich)
library(estimatr)
library(tidyverse)
set.seed=1234
load("data_cleaned/indonesia_facebook_cleaned_weighted.Rda")
d1<-d%>%filter(wave=="wave1")
d%<>%filter(wave=="wave2") ## restrict to 2nd wave,
View(d)
View(d1)
View(d)
